package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.SoulPo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author James
 * @date 2022/1/23 8:40
 **/
public class SoulDAO {
    private static final String
            REPLACE_ONE = "replace into `soul` (`pCode`, `code`, `dataId`,`hero`,`propertyIds`, `quality`, `refine`,`skillIds`, `rePropertyIds`,`reSkillIds`, `propertyLocks`, `skillLocks`,`updateTime`) " +
            "values (?,?,?,?,?,?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `soul` where `pCode` = ?",
            DELETE = "delete from `soul` where `pCode` = ? and `updateTime` != ?";

    public static List<SoulPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, SoulPo.builder);
    }

    public static boolean replace(List<SoulPo> soulPoList) {
        List<Object[]> list = new ArrayList<>();
        for (SoulPo soulPo : soulPoList) {
            Object[] objects = new Object[]{soulPo.getPlayerCode(),soulPo.getCode(),soulPo.getDataId(),soulPo.getHero(),soulPo.getPropertyIds(),soulPo.getQuality(),
                    soulPo.getRefine(),soulPo.getSkillIds(),soulPo.getRePropertyIds(),soulPo.getReSkillIds(),soulPo.getPropertyLocks(),soulPo.getSkillLocks(),soulPo.getUpdateTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == soulPoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});
    }
}
